import Vue from 'vue'
import VueRouter from 'vue-router'
//引入login组件
import Login from '@/components/Login'
import Home from '@/components/Home'
import Welcome from '@/components/Welcome'
import UserList from '@/components/admin/UserList'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      { path: "/welcome", component: Welcome, },
      { path: "/user", component: UserList, }
    ]
  },
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的url
  // from 从哪访问
  // next 继续访问接下来的url next(url) 重定向到url上 next() 继续访问 to路径
  if (to.path == '/login') return next();
  // 获取user
  const userFlag = window.sessionStorage.getItem("user");//取出当前用户
  if (!userFlag) return next('/login');//无值,返回登录页
  next();//符合要求

})

export default router
